import 'package:shared_preferences/shared_preferences.dart';

class LocalStorageUtil {

  static void saveString(String key, String value) async {
//    Future<SharedPreferences> futureSP = SharedPreferences.getInstance();
//    futureSP.then((sp) => sp.setString(key, value));
    SharedPreferences sp = await SharedPreferences.getInstance();
    await sp.setString(key, value);
  }
  static void saveInt(String key, int value) {
    Future<SharedPreferences> futureSP = SharedPreferences.getInstance();
    futureSP.then((sp) => sp.setInt(key, value));
  }
  static void saveBool(String key, bool value) {
    Future<SharedPreferences> futureSP = SharedPreferences.getInstance();
    futureSP.then((sp) => sp.setBool(key, value));
  }
  static void saveDouble(String key, double value) {
    Future<SharedPreferences> futureSP = SharedPreferences.getInstance();
    futureSP.then((sp) => sp.setDouble(key, value));
  }

  static Future<String> getString(String key) async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    return sp.getString(key);
  }
  static Future<int> getInt(String key) async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    return sp.getInt(key);
  }
  static Future<bool> getBool(String key) async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    return sp.getBool(key);
  }
  static Future<double> getDouble(String key) async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    return sp.getDouble(key);
  }

  static void remove(String key) {
    Future<SharedPreferences> futureSP = SharedPreferences.getInstance();
    futureSP.then((sp) => sp.remove(key));
  }
}

class LocalData{

  static const String ACCESS_TOKEN = "access_token";
  static const String REFRESH_TOKEN = "refresh_token";
  static const String TOKEN_TYPE = "token_type";
  static const String EXPIRES_IN = "expires_in";
  static const String UID = "uid";
  static const String IS_LOGIN = "is_login";

  static void saveLoginResponseData(Map<String, dynamic> params) {
    LocalStorageUtil.saveString(ACCESS_TOKEN, params[ACCESS_TOKEN]);
    LocalStorageUtil.saveString(REFRESH_TOKEN, params[REFRESH_TOKEN]);
    LocalStorageUtil.saveString(TOKEN_TYPE, params[TOKEN_TYPE]);
    LocalStorageUtil.saveInt(EXPIRES_IN, params[EXPIRES_IN]);
    LocalStorageUtil.saveInt(UID, params[UID]);
    LocalStorageUtil.saveBool(IS_LOGIN, true);
  }

  static void removeLoginResponseData(Map<String, dynamic> params){
    LocalStorageUtil.remove(ACCESS_TOKEN);
    LocalStorageUtil.remove(REFRESH_TOKEN);
    LocalStorageUtil.remove(TOKEN_TYPE);
    LocalStorageUtil.remove(EXPIRES_IN);
    LocalStorageUtil.remove(UID);
    LocalStorageUtil.remove(IS_LOGIN);
  }
}